<?php


namespace app\api\controller;


use think\Db;

class Trigger extends Lib
{

    // 自定义
    public function zbx_trigger_custom_get()
    {
        $description = [
            "磁盘", "宿主机", "Zabbix", "CPU", "内存",
            "{HOST.NAME}", "process", "More", "重启", "Disk",
            "处理器", "Free", "Datastore", "衰退", "中间件"
        ];

        return $this->zbx_trigger_get($description);
    }

    // 系统
    public function zbx_trigger_system_get()
    {
        $description = ["信息技术部", "智家", "宿主机", "中间件"];

        return $this->zbx_trigger_get($description);
    }

    // 获取trigger
    public function zbx_trigger_get($description)
    {
        $functions = Db::name('functions')->column('triggerid,deptid');

        $params = [
            "output"     => ["triggerid", "description", "priority"],
            "triggerids" => array_keys($functions),
            "search" => [
                "description" => $description
            ],
            "excludeSearch" => true
        ];
        $result = json_decode(self::RPC('trigger.get', $params), true);
        $data = $result['data'];

        if ($data){
            foreach ($data as $k=>&$v){
                $is_trigger = Db::name('triggers')->where('triggerid', $v['triggerid'])->find();
                if ($is_trigger){
                    unset($data[$k]);
                }else{
                    $v['deptid'] = $functions[$v['triggerid']];
                }
            }
            if ($data){
                $add = Db::name('triggers')->insertAll($data);
                if ($add){
                    return true;
                }else{
                    return false;
                }
            }else{
                return true;
            }
        }else{
            return true;
        }
    }
}